package com.iwhalecloud.bss.kite.cucc.service.spec.operate;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.dataservice.mapper.spec.CuccOfferAttrExtMapper;
import com.iwhalecloud.bss.kite.cucc.dataservice.mapper.spec.CuccOfferAttrMapper;
import com.iwhalecloud.bss.kite.dataservice.api.IOperateOfferAttrService;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrSpec;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.dataservice.entity.OfferAttr;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.OfferAttrExt;
import com.iwhalecloud.bss.kite.dataservice.mapper.other.AttrSpecMapper;
import com.iwhalecloud.bss.kite.dataservice.mapper.other.AttrValueMapper;
import com.iwhalecloud.bss.kite.manager.transaction.KiteTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author Jonathan
 */
@Service
public class OperateOfferAttrServiceImpl implements IOperateOfferAttrService {

    @Autowired
    private CuccOfferAttrMapper offerAttrMapper;

    @Autowired
    private CuccOfferAttrExtMapper offerAttrExtMapper;

    @Autowired
    private AttrSpecMapper attrSpecMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @KiteTransactional
    @Override
    public void insertOfferAttr(List<OfferAttr> offerAttrList, List<OfferAttrExt> offerAttrExtList,
                                List<AttrSpec> attrSpecList, List<AttrValue> attrValueList) {
        offerAttrMapper.insertOfferAttrList(offerAttrList);
        if (KiteListUtils.isNotEmpty(offerAttrExtList)) {
            offerAttrExtMapper.insertOfferAttrExtList(offerAttrExtList);
        }
        if (KiteListUtils.isNotEmpty(attrSpecList)) {
            attrSpecMapper.insertAttrSpecList(attrSpecList);
        }
        if (KiteListUtils.isNotEmpty(attrValueList)) {
            attrValueMapper.insertAttrValueList(attrValueList);
        }
    }

    @KiteTransactional
    @Override
    public void updateOfferAttr(List<OfferAttr> offerAttrList, List<OfferAttrExt> offerAttrExtList,
                                List<AttrSpec> attrSpecList, List<AttrValue> attrValueList) {
        offerAttrMapper.updateOfferAttrList(offerAttrList);
        if (KiteListUtils.isNotEmpty(offerAttrExtList)) {
            offerAttrExtMapper.updateOfferAttrExtList(offerAttrExtList);
        }
        if (KiteListUtils.isNotEmpty(attrSpecList)) {
            attrSpecMapper.updateAttrSpecList(attrSpecList);
        }
        if (KiteListUtils.isNotEmpty(attrValueList)) {
            attrValueMapper.updateAttrValueList(attrValueList);
        }
    }

    @KiteTransactional
    @Override
    public void deleteAttrValue(List<String> attrIdList) {
        Example example = new Example(AttrValue.class);
        example.createCriteria().andIn("attrId", attrIdList);
        attrValueMapper.deleteByExample(example);
    }

    @KiteTransactional
    @Override
    public void insertAttrValue(List<AttrValue> attrValueList) {
        attrValueMapper.insertAttrValueList(attrValueList);
    }
}
